<?php

class edit_drivername
{
	var $mess;

	function edit_drivername()
	{
		if(isset($_POST['editRecord']) && isset($_POST['itemID']))
		{
			$this->doEdit();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		if(isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
	}
		
	function doEdit()
	{
		global $db, $lang;
		
		if(isset($_POST['itemID'])) 
		{
			$qUpd = "UPDATE  ". DRIVERNAME_TABLE . ", " . LCUSERS_TABLE . "
					 SET pf_drivername = '" . $_POST['drivername'] . "',
					     u_admin = " . $_POST['admin'] . ", 
						 u_license = " . $_POST['license'] . "
					 WHERE u_id = user_id AND user_id = " . $_POST['itemID'] ;
			echo $qUpd;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['drivername'], $lang['DriverUpdated']), $lang['DriverUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select l_id, l_name
			  From ". LICENSES_TABLE . "
			  ORDER BY l_order ASC";
		$resLic = $db->sql_query($q);
		
		//get the record
		$q = "Select username, u.user_id, pf_drivername as user_drivername, lc.*
			  From ". USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d, " . LCUSERS_TABLE . " lc 
			  Where u.user_id = d.user_id AND u.user_id = lc.u_id AND u.user_id = " . $_POST['itemID'];
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
			$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
			$mess->render();
			
			$licenseOptions = '';
			while($license = $db->sql_fetchrow($resLic)) 
			{
				$licenseOptions .= '<option value="' . $license['l_id'] . '"'. ($row['u_license']==$license['l_id'] ? ' SELECTED>' : '>') . 
											$license['l_name'].'</option>';
			}
			
			$adminOptions = '';
			foreach($lang['DriverAdmin'] as $key => $value)
			{
				$adminOptions .= '<option value="' . $key . '"'. ($row['u_admin']==$key ? ' SELECTED>' : '>') . 
											$value.'</option>';
			}
			
			$userid = $row['user_id'];
			$driver_label = $row['username'] . "'s " . $lang['DriverName'];
			$user_drivername = $row['user_drivername'];
					
			include $skin_dir . 'admin/editdrivername.tpl';
		}
		else
		{
			$mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			$mess->render();
		}
	}

}
?>